package com.hjl.web.servlet;

import com.hjl.domain.Job_inf;
import com.hjl.domain.PageBean;
import com.hjl.service.JobService;
import com.hjl.service.impl.JobServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * 职位表现层
 *
 * @author Lenovo
 * @version 1.0
 * @date 2021/10/8 11:23
 */
@WebServlet("/jobs/*")
public class JobServlet extends BaseServlet {
    private JobService jobService = new JobServiceImpl();
    String prefix = "/WEB-INF/jsp/job/";
    String suffix = ".jsp";

    /**
     * 跳转WEB-INF里的jsp
     *
     * @author Lenovo
     * @date 2021/10/8 22:03
     */
    public void toList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(prefix + "joblist" + suffix).forward(req, resp);
    }

    public void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(prefix + "jobadd" + suffix).forward(req, resp);
    }

    public void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(prefix + "jobedit" + suffix).forward(req, resp);
    }

    /**
     * 分页
     *
     * @author Lenovo
     * @date 2021/10/8 22:04
     */
    public void findJobByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //获取数据（当前页码和行数）
        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        //默认第1页，5行
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        Map<String, String[]> condition = req.getParameterMap();
        PageBean<Job_inf> pb = jobService.findJobByPage(currentPage, rows, condition);
        req.setAttribute("jobs", pb);
        req.setAttribute("condition", condition);

        req.getRequestDispatcher("/jobs/toList").forward(req, resp);
    }

    /**
     * 删除选中
     *
     * @author Lenovo
     * @date 2021/10/8 22:04
     */
    public void delSelected(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] ids = req.getParameterValues("id");
        jobService.delSelect(ids);
        resp.sendRedirect(req.getContextPath() + "/jobs/findJobByPage");
    }

    /**
     * 查询单个（用于编辑）
     *
     * @author Lenovo
     * @date 2021/10/8 22:05
     */
    public void findJob(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String id = req.getParameter("id");
        //调用service方法
        Job_inf job = jobService.findJobById(id);
        //存入request
        req.setAttribute("job", job);
        System.out.println(job);
        //转发到jobedit.jsp
        req.getRequestDispatcher("/jobs/toEdit").forward(req, resp);
    }

    /**
     * 更新
     *
     * @author Lenovo
     * @date 2021/10/8 22:05
     */
    public void updateJob(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //编码
        req.setCharacterEncoding("utf-8");
        //获取map
        Map<String, String[]> map = req.getParameterMap();
        Job_inf job = new Job_inf();
        try {
            //封装
            BeanUtils.populate(job, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
//        调用service方法
        jobService.updateJob(job);
        //重定向回去
        resp.sendRedirect(req.getContextPath() + "/jobs/findJobByPage");
    }

    /**
     * 添加
     *
     * @author Lenovo
     * @date 2021/10/8 22:05
     */
    public void addJob(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        req.setCharacterEncoding("utf-8");
        //获得参数
        Map<String, String[]> map = req.getParameterMap();
        //封装对象
        Job_inf job = new Job_inf();
        try {
            BeanUtils.populate(job, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用service保存
        jobService.addJob(job);
        //跳转到findJobByPage
        resp.sendRedirect(req.getContextPath() + "/jobs/findJobByPage");
    }
}
